{$CLEO .qa}
{$USE CLEO+}
{$USE Audio}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "BMBQA"

// By ArtemQa146

const
    boombox = 31@
    AudioMusic = 30@
    model_id = 29@
    angle_1 = 28@
    coords_x = 27@
    coords_y = 26@
    coords_z = 25@
end

0087: coords_x = 0@ // (float)
0087: coords_y = 1@ // (float)
0087: coords_z = 2@ // (float)
0087: angle_1 = 3@ // (float)

if
    0E4D: random_percent 4@
then
else
    repeat
    wait 0
    until not locate_char_distance_to_coordinates $PLAYER_ACTOR pos coords_x coords_y coords_z radius 150.0
    0A93: terminate_this_custom_script
end


    0209: 1@ = random_int_in_ranges 1 3
    if 1@ == 1
    then model_id = 2226
    else model_id = 2102
    end
    
    model.Load(model_id)
    model.Load(1429)
    if
        not model.Available(1429)
    then
        repeat
        wait 0
        model.Load(1429)
        until model.Available(1429)
    end
    if
        not model.Available(model_id)
    then
        repeat
        wait 0
        model.Load(model_id)
        until model.Available(model_id)
    end
    038B: load_requested_models
    
    0E01: create_object_no_save 1429 at coords_x coords_y coords_z offset 0 ground 1 to boombox
    0F03: create_render_object_to_object boombox model model_id offset 0 0 0 rotation 0 0 0 store_to 1@
    angle_1 += 180.0
    0177: set_object boombox Z_angle_to angle_1
    08D2: object boombox scale_model 0.0
    
    model.Destroy(model_id)
    model.Destroy(1429)
    
    0209: 1@ = random_int_in_ranges 1 9
    0AD3: 5@v = string_format "SQ\M%d.mp3" 1@
    0AC1: AudioMusic = load_audio_stream_with_3d_support 5@v
    0AC3: link_3d_audio_stream AudioMusic to_object boombox
    0AAD: set_audio_stream AudioMusic state 1

    TIMERA = 0
    
    repeat
    wait 0
    if
        03CA:   object boombox exists
    then
    
        if or
            not locate_char_distance_to_object $player_actor object boombox radius 100.0
            0366:   object boombox damaged
        then
            break
        end

    
        //   ,    3       ,   
        0400: store_coords_to 1@ 2@ 3@ from_object boombox with_offset 0.0 0.0 0.0
        0EB2: get_offset_from_camera_in_world_coords 0.0 0.0 0.0 store_to 4@ 5@ 6@
        050A: 1@ = distance_between_XYZ 1@ 2@ 3@ and_XYZ 4@ 5@ 6@
        1@ /= 100.0
        2@ = 0.7
        0063: 2@ -= 1@  // (float)
        if
            2@ < 0.0
        then
            2@ = 0.0
        end
        0ABC: set_audio_stream AudioMusic volume 2@
        
        //     
        if
            0EE6: locate_char_distance_to_object $PLAYER_ACTOR object boombox radius 1.5
        then
            if
                8A2A:   not text_box 'boomb' displayed
            then
                0512: show_permanent_text_box 'boomb'  // If your health reaches zero, you will pass out and you will be treated at the local medical center.
            end
        else
            if
                0A2A:   text_box 'boomb' displayed
            then
                03E6: remove_text_box
            end
        end
        
        
        if and
            0019:   TIMERA > 2000
            0EE6: locate_char_distance_to_object $PLAYER_ACTOR object boombox radius 1.5
            00E1:   player 0 pressed_key 8
        then
            0AAD: set_audio_stream AudioMusic state 2
            0006: TIMERA = 0
        end
        
        //   
        0AB9: get_audio_stream AudioMusic state_to 1@
        if or
            1@ == -1
            1@ == 2
        then
            0AAE: remove_audio_stream AudioMusic
            0209: 1@ = random_int_in_ranges 1 9
            0AD3: 5@v = string_format "SQ\M%d.mp3" 1@
            0AC1: AudioMusic = load_audio_stream_with_3d_support 5@v
            0AC3: link_3d_audio_stream AudioMusic to_object boombox
            0AAD: set_audio_stream AudioMusic state 1
        end
    
    else
        break
    end
    until not player.Defined(0)
    if
        0A2A:   text_box 'boomb' displayed
    then
        03E6: remove_text_box
    end
    0AAE: remove_audio_stream AudioMusic
    if 03CA:   object boombox exists
    then 01C4: remove_references_to_object boombox // This object will now disappear when the player looks away
    end
    
    
    0A93: terminate_this_custom_script
